//
//  HNSelectorHomeWork.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/26.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "SAWAssignHomeworkVC.h"
#import "SAWAlertView.h"
#import "HMStrongBigCategoryView.h"
#import "SAWHomeworkModel.h"
#import "HNSelectorHomeWork.h"
#import "SAWHomeworkTabCell.h"
@interface HNSelectorHomeWork ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSArray *dataSource;
@property (strong, nonatomic) NSMutableArray *contentArr;
@property (strong, nonatomic) NSMutableArray *sunjectsArr;
@property (strong, nonatomic) NSMutableArray *textBooksArr;
@property (strong, nonatomic) NSMutableArray *gradesArr;

@property (strong, nonatomic)UITableView *tableView;
@property (strong, nonatomic)NSArray *XQArr;
@property(nonatomic,copy)NSString *subID;
@property(nonatomic,copy)NSString *graID;
@property(nonatomic,copy)NSString *bookID;
@property(nonatomic,copy)NSString *termID;
@property(nonatomic,copy)NSString *unitCode;
@property(nonatomic,copy)NSString *saveTitle;
@property (strong, nonatomic)SAWHomeworkModel *model;
@end

@implementation HNSelectorHomeWork

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"布置作业";
    self.view.backgroundColor =RGBColor(245, 245, 245);
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(itemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
   
      __weak typeof(self) weakSelf = self;
    [self.model setNetWork:^(NSDictionary *dic) {
        weakSelf.sunjectsArr = dic[@"subjects"];

        weakSelf.gradesArr = dic[@"grades"];

        weakSelf.textBooksArr = dic[@"textBooks"];

    }];
    [self setUI];
}
- (void)itemAction{
    [self.navigationController popViewControllerAnimated:YES];
}
-(SAWHomeworkModel *)model
{
    if (!_model) {
        _model = [[SAWHomeworkModel alloc]init];
    }
    
    return _model;
}
-(NSArray *)XQArr
{
    if (!_XQArr) {
        _XQArr = [[NSArray alloc]initWithObjects:@"上学期",@"下学期", nil];
    }
    
    return _XQArr;
}
-(NSMutableArray *)textBooksArr
{
    if (!_textBooksArr) {
        _textBooksArr = [[NSMutableArray alloc]init];
    }
    
    return _textBooksArr;
}
-(NSMutableArray *)sunjectsArr
{
    if (!_sunjectsArr) {
        _sunjectsArr = [[NSMutableArray alloc]init];
    }
    
    return _sunjectsArr;
}
-(NSMutableArray *)gradesArr
{
    if (!_gradesArr) {
        _gradesArr = [[NSMutableArray alloc]init];
    }
    
    return _gradesArr;
}


-(NSMutableArray *)contentArr
{
    if (!_contentArr) {
        _contentArr = [[NSMutableArray alloc]initWithObjects:@"请输入作业题目",@"请选择科目",@"请选择教材",@"请选择年级",@"请选择学期",@"请选择单元", nil];
    }
    
    return _contentArr;
}
-(NSArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [[NSArray alloc]initWithObjects:@"作业题目",@"学习科目",@"教材",@"年级",@"学期", @"单元",nil];
    }
    
    return _dataSource;
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];//设置标题颜色
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}]; //设置标题颜色
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色
}
-(void)setUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, kScreenWidth-20, kScreenHeight-kTopHeight-64)];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.rowHeight=90;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[SAWHomeworkTabCell class] forCellReuseIdentifier:@"Cell"];
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    UIButton *comitBtn = [[UIButton alloc]init];
    [self.view addSubview:comitBtn];
    [comitBtn setTitle:@"确定" forState:0];
    [comitBtn setBackgroundImage:[UIImage imageNamed:@"btn_ensure_default_See_Teacher"] forState:0];
    [comitBtn addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
    [comitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.bottom.offset(-10);
        make.height.offset(40);
    }];
}
-(void)saveData
{
    if (self.unitCode.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请先选择作业选项!"];
    }
    else
    {
        if (self.saveTitle.length==0) {
            [SVProgressHUD showErrorWithStatus:@"请先输入作业题目!"];
        }
        else
        {
            [self.model savaData:@[self.lessonID,self.saveTitle,self.Uids] withData:^(NSString *homeworkID) {
                SAWAssignHomeworkVC *vc = [SAWAssignHomeworkVC new];
                vc.homeworkID = [NSString stringWithFormat:@"%@",homeworkID];
                vc.uniCode = self.unitCode;
                [self.navigationController pushViewController:vc animated:YES];
            }];
    
            
        }
        
    }
   
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SAWHomeworkTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.Btn.tag = indexPath.row +1166;
    [cell.Btn addTarget:self action:@selector(selectorData:) forControlEvents:UIControlEventTouchUpInside];
    if (indexPath.row==0) {
        if (cell.edicell) {
            cell.edicell();
        }
        [cell.filed addTarget:self action:@selector(saveTitleData:) forControlEvents:UIControlEventEditingDidEnd];
        cell.filed.placeholder = self.contentArr[0];
        
        if(self.saveTitle.length>0)
        {
            cell.filed.text = self.saveTitle;
        }
    }
   
    [cell.Btn setTitle:self.contentArr[indexPath.row] forState:0];
    cell.titLab.text=self.dataSource[indexPath.row];
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)saveTitleData:(UITextField *)filed
{
    self.saveTitle = filed.text;
}
-(void)selectorData:(UIButton *)sender
{
   
  // SAWHomeworkTabCell *cell = [sender superview];
   // NSIndexPath *cellPath = [self.tableView indexPathForCell:cell];
    
    if (sender.tag==1167) {
  
        SAWAlertView *alert = [[SAWAlertView alloc]initWithdataArr:self.sunjectsArr withType:1];
        alert.selecItem = ^(NSString *item,NSString *type) {
            self.subID =[NSString stringWithFormat:@"%@",type];
            [self.contentArr replaceObjectAtIndex:sender.tag-1166 withObject:item];
            [_tableView reloadData];
        };
    [alert show];
    }
    if (sender.tag ==1171) {
        if (_subID.length==0) {
            [SVProgressHUD showErrorWithStatus:@"请先选择科目"];
        }
       else if (_bookID.length==0) {
            [SVProgressHUD showErrorWithStatus:@"请先选择教材"];
        }
       else if (_graID.length==0) {
           [SVProgressHUD showErrorWithStatus:@"请先选择年级"];
        }
       
      else if (_termID.length==0) {
           [SVProgressHUD showErrorWithStatus:@"请先选择学期"];
       }
        else
        {
            NSArray *arr = @[_subID,_bookID,_graID,_termID];
            
            [self.model getUserXQ:arr withData:^(NSArray *array) {
                
                SAWAlertView *alert = [[SAWAlertView alloc]initWithdataArr:array withType:6];
                alert.selecItem = ^(NSString *item,NSString *type) {
                   [self.contentArr replaceObjectAtIndex:sender.tag-1166 withObject:item];
                    self.unitCode = [NSString stringWithFormat:@"%@",type];;
                    [_tableView reloadData];
                };
                [alert show];
            }];
            
        }
    }
    if (sender.tag ==1168) {
        SAWAlertView *alert = [[SAWAlertView alloc]initWithdataArr:self.textBooksArr withType:2];
        alert.selecItem = ^(NSString *item,NSString *type) {
            self.bookID = [NSString stringWithFormat:@"%@",type];
            [self.contentArr replaceObjectAtIndex:sender.tag-1166 withObject:item];
            [_tableView reloadData];
        };
        [alert show];
    }
    if (sender.tag ==1169) {
        SAWAlertView *alert = [[SAWAlertView alloc]initWithdataArr:self.gradesArr withType:3];
        alert.selecItem = ^(NSString *item,NSString *type) {
              self.graID = [NSString stringWithFormat:@"%@",type];;
            [self.contentArr replaceObjectAtIndex:sender.tag-1166 withObject:item];
            [_tableView reloadData];
        };
        [alert show];
    }
    if (sender.tag ==1170) {
    SAWAlertView *alert = [[SAWAlertView alloc]initWithdataArr:self.XQArr withType:100];
    alert.selecItem = ^(NSString *item,NSString *type) {
        [self.contentArr replaceObjectAtIndex:sender.tag-1166 withObject:item];
        if ([item isEqualToString:@"上学期"]) {
            self.termID = @"1";
        }
        if ([item isEqualToString:@"下学期"]) {
            self.termID = @"2";
        }
        [_tableView reloadData];
    };
        [alert show];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
